home *** CD-ROM | disk | FTP | other *** search
/ .net 1997 May / net 32 / NET32PC.iso / mac / NET32PPC / NET32PPC.dxr / 00407_parse.ls < prev    next >
Encoding:
Text File  |  1997-04-11  |  2.5 KB  |  106 lines

  1. on parse chunk, alistcast
  2.   set l to chunk
  3.   set out to EMPTY
  4.   set alist to [0: #null]
  5.   set i to 1
  6.   repeat while not (l = EMPTY)
  7.     if char 1 of l = "<" then
  8.       delete char 1 of l
  9.       set tag to EMPTY
  10.       repeat while not ((l = EMPTY) or (char 1 of l = ">"))
  11.         if char 1 of l = QUOTE then
  12.           set tag to tag & QUOTE & ""E&" & QUOTE
  13.         else
  14.           set tag to tag & char 1 of l
  15.         end if
  16.         delete char 1 of l
  17.       end repeat
  18.       addProp(alist, i, tag)
  19.       if not (l = EMPTY) then
  20.         delete char 1 of l
  21.       end if
  22.       next repeat
  23.     end if
  24.     set out to out & char 1 of l
  25.     delete char 1 of l
  26.     set i to i + 1
  27.   end repeat
  28.   deleteProp(alist, 0)
  29.   set the text of cast alistcast to string(parse2(alist))
  30.   return out
  31. end
  32.  
  33. on parse2 alist
  34.   addProp(alist, #null, "0")
  35.   set i to 1
  36.   repeat while not (getPropAt(alist, i) = #null)
  37.     set tag to getAt(alist, i)
  38.     if nameof(tag) = "a" then
  39.       set closed to 0
  40.       put i & "anchor tag opened: " & QUOTE & restof(tag) & QUOTE
  41.       setAt(alist, i, restof(tag))
  42.       set i to i + 1
  43.       repeat while not closed
  44.         set tag to getAt(alist, i)
  45.         if nameof(tag) = "/a" then
  46.           set closed to 1
  47.           put i & "anchor tag closed"
  48.           setAt(alist, i, EMPTY)
  49.           set i to i + 1
  50.           next repeat
  51.         end if
  52.         if getPropAt(alist, i) = #null then
  53.           put "warning: eof and tag not closed."
  54.           put i & " Forcing closed"
  55.           deleteProp(alist, #null)
  56.           addProp(alist, 25000, EMPTY)
  57.           addProp(alist, #null, "0")
  58.           set closed to 1
  59.           set i to i + 1
  60.           next repeat
  61.         end if
  62.         put "tag not closed, ignoring tag: " & QUOTE & nameof(tag) & QUOTE
  63.         deleteAt(alist, i)
  64.       end repeat
  65.       next repeat
  66.     end if
  67.     put "unknown tag " & QUOTE & nameof(tag) & QUOTE
  68.     deleteAt(alist, i)
  69.   end repeat
  70.   deleteProp(alist, #null)
  71.   return alist
  72. end
  73.  
  74. on nameof t
  75.   return car_(stripfrombegining(t, " "), " ")
  76. end
  77.  
  78. on restof t
  79.   return stripfrombegining(cdr_(stripfrombegining(t, " "), " "), " ")
  80. end
  81.  
  82. on car_ l, delimiter
  83.   set save to the itemDelimiter
  84.   set the itemDelimiter to delimiter
  85.   set a to item 1 of l
  86.   set the itemDelimiter to save
  87.   return a
  88. end
  89.  
  90. on cdr_ l, delimiter
  91.   set save to the itemDelimiter
  92.   set the itemDelimiter to delimiter
  93.   delete item 1 of l
  94.   set the itemDelimiter to save
  95.   return l
  96. end
  97.  
  98. on stripfrombegining l, a
  99.   if char 1 of l = a then
  100.     repeat while char 1 of l = a
  101.       delete char 1 of l
  102.     end repeat
  103.   end if
  104.   return l
  105. end
  106.